# -*- coding: utf-8 -*-
import pandas as pd

#return list( (catagroy, translation) )

# ==============================original method ==================================================
def get_catagory_keyword_list_original():
    f = open('Data/dict/interest.csv')
    lines = f.readlines()

    # list(key,string)
    catagroy_keyword_list = []
    for line in lines:
        s = line.split(',')
        if len(s) == 2:
            catagroy_keyword_list.append((s[0].strip(),s[1].strip()))
    return catagroy_keyword_list

# ==============================current method ==================================================
def preprocess(data):
        data['Class1'] = 'Interest'
        Current_Class2 = 'Nothing'
        Current_Class3 = 'Nothing'
        for index,row in data.iterrows():
    
            Class2 = row['Class2']
            Class3 = row['Class3']
    
            if type(Class2) != float:
                Current_Class2 = Class2
                Current_Class3 = 'Nothing'
            elif Current_Class2 != 'Nothing':
                data.loc[index,'Class2'] = Current_Class2
    
            if type(Class3) != float:
                Current_Class3 = Class3
            elif Current_Class3 != 'Nothing':
                data.loc[index,'Class3'] = Current_Class3
         
        return data


def get_catagory_keyword_list_now():
    
    NAME = 'docs/17.09.08.interest description [Chinese] [URL Example].xlsx'
    COLUMNS = ['Class1','Class2','Class2_Chinese','Class3','Class3_Chinese','Search_Keyword','Des_En','Des_Ch','Other','Title','Website']

    data = pd.read_excel(NAME,sheetname='Sheet2')
    data.columns = COLUMNS
    data = preprocess(data)
    
    index_chinese = []
    for _,row in data.iterrows():
        class1 = row['Class1']
        class3 = row['Class3']
        Chinese = row['Search_Keyword']
        if type(class3) == float:
            class2 = row['Class2'].split(' ')[0]
            Index = class1 + '.' + class2
        else:
            class3 = row['Class3'].split(' ')[0]
            Index = class1 + '.' + class2 + '.' + class3
        
        index_chinese.append((Index,Chinese))
    return index_chinese

# ================================================================================================
# ================================================================================================
def get_catagory_keyword_list(source):
    if source == 'original':
        return get_catagory_keyword_list_original()
    elif source == 'now':
        return get_catagory_keyword_list_now()
    
    
    
    
    